package com.nation.controller;

import com.nation.domain.User;
import com.nation.serviceImpl.InternationalDataServiceImpl;
import com.nation.utils.HandelExcel;
import com.nation.utils.HandleZIP;
import com.nation.utils.ReadAndStoreFile;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dqd on 2017/11/16.
 */
@Controller
@RequestMapping(value = "/InternationalData")
public class InternationalDataController {
    @Autowired
    InternationalDataServiceImpl InternationalDataService;
    @RequestMapping(value = "internationaldata")
    public String internationaldata(){
        return "InternationalData/internationaldata";
    }

    @RequestMapping(value = "uploadInternalData")
    @ResponseBody
    public String uploadInternalData(HttpServletRequest request, @RequestParam(value = "file") MultipartFile multipartFile,@RequestParam(value = "file_type")int file_type){
        List<String> file_paths=new ArrayList<String>();
        org.apache.shiro.subject.Subject currentuser= SecurityUtils.getSubject();
        User user=(User)currentuser.getPrincipal();
        String input_code=user.getYHZH();

        try {
            String path=request.getSession().getServletContext().getResource("/").getPath();
            String file_path=path+ HandleZIP.INTERNATIONALDATAS;



            System.out.println("path--"+file_path);

//          将MultipartFile格式转换成File
            CommonsMultipartFile cf= (CommonsMultipartFile)multipartFile; //这个picture_ZIP是MultipartFile的
            DiskFileItem fi = (DiskFileItem)cf.getFileItem();
            File file = fi.getStoreLocation();
            file_paths=HandleZIP.unzip( file,file_path);

            for(String excel_path: file_paths){
                HandelExcel poi = new HandelExcel();
                List<List<String>> list = poi.read(excel_path);
                switch(file_type){
                    case 1: break;
                    case 2: break;
                    case 3: break;
                    case 4: break;
                }
                // InternationalDataService.uploadInternalData(list,input_code);
            }



        } catch (IOException e) {
            e.printStackTrace();
            return "上传失败";
        }
        return "上传成功";

    }
}
